package com.qiguliuxing.dts.admin.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.constraints.NotNull;

import com.qiguliuxing.dts.admin.annotation.RequiresPermissionsDesc;
import com.qiguliuxing.dts.common.constant.ApiAdminConsts;
import com.qiguliuxing.dts.common.constant.LogParamsConsts;
import com.qiguliuxing.dts.core.annotation.aoplog.WebLog;
import com.qiguliuxing.dts.service.base.DtsAdService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.qiguliuxing.dts.common.util.ResponseUtils;
import com.qiguliuxing.dts.core.validator.Order;
import com.qiguliuxing.dts.core.validator.Sort;
import com.qiguliuxing.dts.db.domain.DtsAd;

/**
 * 广告管理控制器
 * @author qiguliuxing
 */
@Validated
@RestController
@RequestMapping("/admin/ad")
@Api(tags = ApiAdminConsts.Ad.PARENT)
public class AdminAdController {
	private static final Logger logger = LoggerFactory.getLogger(AdminAdController.class);

	@Autowired
	private DtsAdService adService;

    @GetMapping("/list")
	@RequiresPermissions("admin:ad:list")
	@RequiresPermissionsDesc(menu = { "推广管理", "广告管理" }, button = "查询")
    @ApiOperation(value = ApiAdminConsts.Ad.LIST)
    @WebLog(description = ApiAdminConsts.Ad.LIST, excludes = {LogParamsConsts.RESPONSE})
	public Object list(@ApiParam(value = "名称") String name,
					   @ApiParam(value = "内容") String content,
					   @ApiParam(value = "页码",defaultValue = "1") @RequestParam(defaultValue = "1") Integer page,
					   @ApiParam(value = "分页大小",defaultValue = "10") @RequestParam(defaultValue = "10") Integer limit,
					   @ApiParam(value = "排序字段",defaultValue = "add_time") @Sort @RequestParam(defaultValue = "add_time") String sort,
					   @ApiParam(value = "排序规则",defaultValue = "desc") @Order @RequestParam(defaultValue = "desc") String order) {
		List<DtsAd> adList = adService.querySelective(name, content, page, limit, sort, order);
		long total = PageInfo.of(adList).getTotal();
		Map<String, Object> data = new HashMap<>();
		data.put("total", total);
		data.put("items", adList);

		return ResponseUtils.ok(data);
	}

	private Object validate(DtsAd ad) {
		String name = ad.getName();
		if (StringUtils.isEmpty(name)) {
			return ResponseUtils.badArgument();
		}
		String content = ad.getContent();
		if (StringUtils.isEmpty(content)) {
			return ResponseUtils.badArgument();
		}
		return null;
	}

	@PostMapping("/create")
	@RequiresPermissions("admin:ad:create")
	@RequiresPermissionsDesc(menu = { "推广管理", "广告管理" }, button = "添加")
	@ApiOperation(value = ApiAdminConsts.Ad.CREATE)
    @WebLog(description = ApiAdminConsts.Ad.CREATE, persistence = true)
	public Object create(@RequestBody DtsAd ad) {
		Object error = validate(ad);
		if (error != null) {
			logger.error("广告管理 添加校验不通过!");
			return error;
		}
		adService.add(ad);

		return ResponseUtils.ok(ad);
	}

	@GetMapping("/read")
	@RequiresPermissions("admin:ad:read")
	@RequiresPermissionsDesc(menu = { "推广管理", "广告管理" }, button = "详情")
	@ApiOperation(value = ApiAdminConsts.Ad.READ)
    @WebLog(description = ApiAdminConsts.Ad.READ)
	public Object read(@ApiParam(value = "广告id") @NotNull Integer id) {
		DtsAd dtsAd = adService.findById(id);
		return ResponseUtils.ok(dtsAd);
	}

	@PostMapping("/update")
	@RequiresPermissions("admin:ad:update")
	@RequiresPermissionsDesc(menu = { "推广管理", "广告管理" }, button = "编辑")
	@ApiOperation(value = ApiAdminConsts.Ad.UPDATE)
    @WebLog(description = ApiAdminConsts.Ad.UPDATE, persistence = true)
	public Object update(@RequestBody DtsAd ad) {
		Object error = validate(ad);
		if (error != null) {
			return error;
		}
		if (adService.updateById(ad) == 0) {
			logger.info("推广管理->广告管理->编辑,更新广告数据失败!");
			return ResponseUtils.updatedDataFailed();
		}
		return ResponseUtils.ok(ad);
	}

	@PostMapping("/delete")
	@RequiresPermissions("admin:ad:delete")
	@RequiresPermissionsDesc(menu = { "推广管理", "广告管理" }, button = "删除")
	@ApiOperation(value = ApiAdminConsts.Ad.DELETE)
    @WebLog(description = ApiAdminConsts.Ad.DELETE, persistence = true)
	public Object delete(@RequestBody DtsAd ad) {
		Integer id = ad.getId();
		if (id == null) {
			return ResponseUtils.badArgument();
		}
		adService.deleteById(id);

		return ResponseUtils.ok();
	}

}
